package cn.boy.gui.adminstrator;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import cn.boy.adapter.DormitoryAdapter;
import cn.boy.domain.Dormitory;
import cn.boy.home.R;
import cn.boy.service.HomeService;

public class DormitoryListActivity extends Activity {
	
	protected static final String TAG = "DormitoryListActivity";

	String no_prex; // dormitory no prex
	String retValue;	// return value of json from server
	List<Dormitory> dorms;
	
	ProgressDialog pd;
	
	ListView listView;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.dormitoy_list);
		
		Intent intent = getIntent();
		no_prex = intent.getStringExtra("no_prex");
		
		findViews();
		
		dorms = new ArrayList<Dormitory>();
		
		initData();
		
		listView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				final Dormitory dorm = (Dormitory) parent.getItemAtPosition(position);
				final Dialog dialog = new Dialog(DormitoryListActivity.this);
				LayoutInflater inflater = LayoutInflater.from(DormitoryListActivity.this);
				final View dialogView = inflater.inflate(R.layout.dialog_mark, null);
				dialog.setContentView(dialogView);
				dialog.setTitle(dorm.getName());
				dialog.show();
				
				final EditText txtMark = (EditText)dialogView.findViewById(R.id.et_dorm_mark);
				Button yes = (Button)dialogView.findViewById(R.id.btn_yes);
				yes.setOnClickListener(new OnClickListener() {
					
					public void onClick(View v) {
						
						new Thread(new Runnable() {
							
							public void run() {
								try {
									String mark = txtMark.getText().toString();
									Dormitory _dorm = new Dormitory(dorm.getNO(), dorm.getName(), mark, dorm.getDate());
									HomeService.updateDormitoryMark(_dorm);
								} catch (Exception e) {
									Log.e(TAG,e.toString());
								}
							}
						}).start();
						dialog.dismiss();
						
					}
				});
				Button no = (Button)dialogView.findViewById(R.id.btn_no);
				no.setOnClickListener(new OnClickListener() {
					
					public void onClick(View v) {
						dialog.dismiss();
					}
				});
			}
		});
	}

	private void initData() {
		showDialog();
		new Thread(new Runnable() {
			public void run() {
				try {
					retValue = HomeService.queryDormitoryByPrex(no_prex);
					Log.i(TAG, "retValue---->>" + retValue);
					if(!retValue.equals("")){
						sendHandlerMessage(1);
					}else{
						sendHandlerMessage(2);
					}
				} catch (Exception e) {
					Log.e(TAG, e.toString());
					sendHandlerMessage(0);
				}
			}
		}).start(); // must start
	}

	private void findViews() {

		listView = (ListView)findViewById(R.id.list_dormitory_prex);
	}
	
	private void showDialog() {
		pd = new ProgressDialog(DormitoryListActivity.this); 
		pd.setMessage(getResources().getString(R.string.get_dorms_ing));
		pd.show();
	}
	
	private void sendHandlerMessage(int what) {
		Message msg = handler.obtainMessage(what);
		handler.sendMessage(msg);
		
	}
	
	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			if(pd!=null){
				pd.dismiss();
			}
			switch(msg.what){
			case 0:
				Toast.makeText(DormitoryListActivity.this, R.string.net_unreacgabke, 1).show();
				break;
			case 1:
				try {
					JSONArray array = new JSONArray(retValue);
					for(int i = 0;i < array.length();i++){
						JSONObject obj = array.getJSONObject(i);
						Dormitory dorm = mappingEntity(obj);
						dorms.add(dorm);
					}
					DormitoryAdapter adapter = new DormitoryAdapter(DormitoryListActivity.this, dorms);
					listView.setAdapter(adapter);
				} catch (JSONException e) {
					Log.e(TAG, e.toString());
				}
				break;
			case 2:
				Toast.makeText(DormitoryListActivity.this, R.string.get_data_fail, 1).show();
				break;
			}
		}

		
	};

	private Dormitory mappingEntity(JSONObject obj) {
		
		try {
			String no = obj.getString("no");
			String name = obj.getString("name");
			String mark = obj.getString("mark");
			String date = obj.getString("date");
			Dormitory dorm = new Dormitory(no, name, mark, date);
			return dorm;
		} catch (JSONException e) {
			Log.e(TAG, e.toString());
			return null;
		}
		
	};
}
